package com.uenpay.im.ui.old.activity.movement;

import android.text.TextUtils;

import com.qyx.android.utilities.DateTimeUtility;
import com.uenpay.im.configuration.APIConfiguration;
import com.uenpay.im.entities.HttpInvokeResult;
import com.uenpay.im.invokeitems.BaseHttpInvokeItem;
import com.uenpay.im.utilities.QyxDateUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class GetMovementListInvokeItem extends BaseHttpInvokeItem {
	public class GetMovementListInvokeItemResult extends HttpInvokeResult {
		ArrayList<MovementEntity> arrayList = new ArrayList<MovementEntity>();
	}

	public GetMovementListInvokeItemResult getOutput() {
		return (GetMovementListInvokeItemResult) GetResultObject();
	}

	/**
	 * 获取活动列表
	 * 
	 * @param type
	 *            1：所有活动2：我参加的活动3：我创建的活动
	 * @param page
	 * @param cust_id
	 */
	public GetMovementListInvokeItem(int type, int page, String cust_id) {
		String url = "";
		if (type == 1) {
			url = APIConfiguration.getBaseUrl()
					+ "Activity/Activity/getActivityList?custid=" + cust_id
					+ "&pageIndex=" + page + "&pageSize=12&"
					+ APIConfiguration.getCustIdAndToken();
		} else if (type == 2) {
			url = APIConfiguration.getBaseUrl()
					+ "Activity/Activity/mySignup?pageIndex=" + page
					+ "&pageSize=12&" + APIConfiguration.getCustIdAndToken();
		} else if (type == 3) {
			url = APIConfiguration.getBaseUrl()
					+ "Activity/Activity/myIssuance?pageIndex=" + page
					+ "&pageSize=12&" + APIConfiguration.getCustIdAndToken();
		}

		SetUrl(url);
	}

	@Override
	protected Object DeserializeResult(String responseBody) {
		GetMovementListInvokeItemResult result = new GetMovementListInvokeItemResult();
		JSONObject json = null;
		try {
			json = new JSONObject(responseBody);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if (json == null) {
			return null;
		}
		result.status = json.optInt("status");
		result.msg = json.optString("msg");
		if (result.status != 0) {
			return result;
		}

		JSONArray arr = json.optJSONArray("data");
		if (arr != null) {
			for (int i = 0, size = arr.length(); i < size; i++) {
				result.arrayList.add(desMovementEntity(arr.optJSONObject(i)));
			}
		}

		return result;
	}

	private MovementEntity desMovementEntity(JSONObject json) {
		MovementEntity movementEntity = new MovementEntity();
		movementEntity.movementAddress = json.optString("address");
		movementEntity.movementTitle = json.optString("title");
		movementEntity.movementPublishCustId = json.optString("custid");
		movementEntity.movementPublishNickName = json.optString("nickname");
		movementEntity.movementStartTime = json.optString("starttime");
		movementEntity.movementFee = json.optString("fee");
		movementEntity.movementDescription = json.optString("description");
		movementEntity.movementCreateTime = json.optString("createtime");
		movementEntity.movementPersonNum = json.optInt("personnum");
		movementEntity.movementId = json.optInt("activityid");
		JSONObject coverJson = json.optJSONObject("cover");
		if (coverJson != null && coverJson.has("fileid")
				&& coverJson.has("filehash")) {
			movementEntity.movementCoverFileHash = coverJson
					.optString("filehash");
			movementEntity.movementCoverFileId = coverJson.optString("fileid");
		}
		movementEntity.movementPublishSecond = json.optString("starttime")
				.substring(11, 16);
		if (!TextUtils.isEmpty(movementEntity.movementCreateTime)) {
			if (movementEntity.movementCreateTime.length() > 11) {
				movementEntity.movementCreateTime = movementEntity.movementCreateTime
						.substring(5, 10);
			}
		}

		if (!TextUtils.isEmpty(movementEntity.movementStartTime)) {
			if (movementEntity.movementStartTime.length() > 10) {
				movementEntity.movementStartTime = movementEntity.movementStartTime
						.substring(0, 11);
			}
		}

		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss",
				Locale.getDefault());
		Date date;
		try {
			date = format.parse(json.optString("starttime"));
			Calendar targetCalendar = DateTimeUtility
					.convertDateToCalendar(date);
			movementEntity.movementPublishWeekDay = QyxDateUtils
					.getWeekDateString(targetCalendar);
		} catch (ParseException e) {
			e.printStackTrace();
		}

		return movementEntity;
	}

}
